/*
 * $Id: dictGui.cpp 9ce65bed8ef4 2012/08/26 02:14:51 pgurumur $
 * Copyright (c) 2012 Prabhu Gurumurthy <pgurumur@gmail.com>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *

 * __version__  = "$Revision: 9ce65bed8ef4 $"
 * __author__   = "$Author: pgurumur $"
 * __modified__ = "$Date: 2012-08-26 02:14:51Z $"
 */
#include "dictGui.h"

namespace QtDict { 
   qtDict::qtDict(QWidget *parent = 0) : QWidget(parent), __conn(0) 
   {
      _mainLayout = new QGridLayout;

      _editLayout = new QGroupBox;
      _buttonLayout = new QGroupBox;
      _resultLayout = new QGroupBox;

      _mainLayout->addWidget(_editLayout);
      _mainLayout->addWidget(_buttonLayout);
      _mainLayout->addWidget(_resultLayout);

      _ipv4 = new QRadioButton("ipv4");
      _ipv6 = new QRadioButton("ipv6");
   
      QString server = "dictionary server: ";
      _label = new QLabel(server);
      _textEdit = new QLineEdit(this);
      _connectButton = new QPushButton("Connect");
      connect(_connectButton, SIGNAL(clicked()), this, SLOT(dictConnect()));

      _layout = new QHBoxLayout;
      _layout->addWidget(_ipv4);
      _layout->addWidget(_ipv6);
      _layout->addWidget(_label);
      _layout->addWidget(_textEdit);
      _layout->addWidget(_connectButton);
   
      _editLayout->setLayout(_layout);

      QStringList queryWordList; 
      queryWordList << "define" << "match" << "show" << "option" << "status" 
         << "databases" << "strategies" << "info" << "server" << "re" << "help";
      
      QString qLabel = "Query dictionary: ";
      _queryLabel = new QLabel(qLabel);
      _queryEdit = new QLineEdit(this);
      _queryCompleter = new QCompleter(queryWordList, this);
      _queryCompleter->setCaseSensitivity(Qt::CaseInsensitive);
      _queryEdit->setCompleter(_queryCompleter);
      _queryButton = new QPushButton("Query");
      _quitButton = new QPushButton("Quit");
      connect(_queryButton, SIGNAL(clicked()), this, SLOT(dictQuery()));
      connect(_quitButton, SIGNAL(clicked()), qApp, SLOT(quit()));

      _bLayout = new QHBoxLayout;
      _bLayout->addWidget(_queryLabel);
      _bLayout->addWidget(_queryEdit);
      _bLayout->addWidget(_queryButton);
      _bLayout->addWidget(_quitButton);

      _buttonLayout->setLayout(_bLayout);

      _vLayout = new QVBoxLayout;
      QString resultString = "Query result: ";
      _resultLabel = new QLabel(resultString);
      _resultEdit = new QTextEdit();
      _vLayout->addWidget(_resultLabel);
      _vLayout->addWidget(_resultEdit);
      _resultLayout->setLayout(_vLayout);

      setLayout(_mainLayout);
   }

   qtDict::~qtDict()
   {
      if (_ipv4)
         delete _ipv4;

      if (_ipv6)
         delete _ipv6;

      if (_label)
         delete _label;

      if (_textEdit)
         delete _textEdit;

      if (_connectButton)
         delete _connectButton;

      if (_queryEdit)
         delete _queryEdit;

      if (_queryLabel)
         delete _queryLabel;

      if (_queryButton)
         delete _queryButton;

      if (_quitButton)
         delete _quitButton;

      if (_bLayout)
         delete _bLayout;

      if (_layout)
         delete _layout;
   
      if (_editLayout)
         delete _editLayout;

      if (_buttonLayout)
         delete _buttonLayout;

      if (_mainLayout)
         delete _mainLayout;

      if (_client)
         delete _client;

      if (_resultLayout)
         delete _resultLayout;

      if (_vLayout)
         delete _vLayout;

      if (_resultLabel)
         delete _resultLabel;

      if (_resultEdit)
         delete _resultEdit;

      if (_queryCompleter)
         delete _queryCompleter;
   }

   void
   qtDict::dictConnect()
   {
      QString readBuffer;

      int af = 0;
      if (_ipv4->isChecked())
         af = AF_INET;
      else if (_ipv6->isChecked())
         af = AF_INET6;

      if (__conn == 0) { 
         _client = new dictClient (_textEdit->text(), DictPort, af); 
         _client->dictConnect (); 
         if (_client->readSocket(readBuffer) == 0) 
            _resultEdit->insertPlainText(readBuffer);

         __conn = 1;
      }
   }

   void
   qtDict::dictQuery()
   {
      QString query = _queryEdit->text() + "\r\n";
      if (_client->writeSocket(query) >= 0) {
         QString retval;
         if (_client->readSocket(retval) == 0) {
            _resultEdit->clear();
            _resultEdit->textCursor().insertText(retval);
         }
      }
   }
}

